package com.ita.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ita.dao.BookDao;
import com.ita.pojo.Book;
import com.ita.util.DBUtil;

public class BookDaoImpl implements BookDao {

	@Override
	public List<Book> findAllBook() {
		
		List<Book> list = new ArrayList<Book>();
		String sql = "select * from book";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DBUtil.createByJNDI();
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				Book b = new Book();
				b.setId(rs.getInt("id"));
				b.setBookname(rs.getString("bookname"));
				b.setPath(rs.getString("path"));
				list.add(b);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, rs);
		}
		return list;
	}

	@Override
	public Book getBook(Integer id) {
		String sql = "select * from book where id=?";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		Book b = null;
		try {
			con = DBUtil.createByJNDI();
			pst = con.prepareStatement(sql);
			pst.setInt(1, id);
			rs = pst.executeQuery();
			if (rs.next()) {
				b = new Book();
				b.setId(rs.getInt("id"));
				b.setBookname(rs.getString("bookname"));
				b.setPath(rs.getString("path"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, rs);
		}
		return b;
	}
	
	

}
